// ---------------------------------------------------------------------------
//Copyright (C) 2008-2011 Nokia Corporation and/or its subsidiary(-ies).
//All rights reserved.
//Contact: Nokia Corporation (qt-info@nokia.com)

//You may use the files in this folder under the terms of the BSD
//license as follows:

//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions are
//met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in
//    the documentation and/or other materials provided with the
//    distribution.
//  * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
//    the names of its contributors may be used to endorse or promote
//    products derived from this software without specific prior written
//    permission.

//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
//OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// ---------------------------------------------------------------------------

.pragma library

var _db;

function openDB()
{
    _db = openDatabaseSync("fuleDB","1.0","the fuel db",1000000);

    createTable();
}
function reOpenDB()
{
    _db = openDatabaseSync("fuleDB","1.0","the fuel db",1000000);
}

function createTable()
{
    _db.transaction(
                function(tx){
                    tx.executeSql("CREATE TABLE IF NOT EXISTS fuel (id INTEGER PRIMARY KEY AUTOINCREMENT,qty TEXT,cost TEXT, date TEXT, title TEXT, note TEXT)");
                }
                );
}
function emptyTable()
{
    _db.transaction(
                function(tx){
                    tx.executeSql("delete from fuel");
                }
                );
}
function createSettings()
{
    _db.transaction(
                function(tx){
                    tx.executeSql("CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY AUTOINCREMENT,viewType TEXT,currency TEXT)");
                }
                );
}

function dropTable()
{
    _db.transaction(
                function(tx){
                    tx.executeSql("DROP TABLE IF EXISTS fuel");
                }
                )
}

function readFuelItems()
{
    var data = [];
    _db.readTransaction(
                function(tx){
                    var rs = tx.executeSql("SELECT * FROM fuel ORDER BY date DESC");
                    for (var i=0; i< rs.rows.length; i++) {
                        data[i] = rs.rows.item(i);
                    }

                }
                );
    return data;
}
function readSettings()
{
    var data = [];
    _db.readTransaction(
                function(tx){
                    var rs = tx.executeSql("SELECT * FROM settings");
                    for (var i=0; i< rs.rows.length; i++) {
                        data[i] = rs.rows.item(i);
                    }

                }
                );
    return data;
}
function updateSettings(settings)
{
    _db.transaction(
                function(tx){
                    tx.executeSql("UPDATE settings SET curr = ? ,qty = ? \
                                  ,title = ?, note = ? \
                                  WHERE id = ?", [fuelItem.cost,fuelItem.qty,fuelItem.title, fuelItem.note,fuelItem.id]);
                }
                )
}

function updateFuelItem(fuelItem)
{
    _db.transaction(
                function(tx){
                    tx.executeSql("UPDATE fuel SET cost = ? ,qty = ? ,date=? \
                                  , note = ? \
                                  WHERE id = ?", [fuelItem.cost,fuelItem.qty,fuelItem.date, fuelItem.note,fuelItem.id]);
                }
                )
}

function deleteFuelItem(id)
{
    _db.transaction(
                function(tx){
                    tx.executeSql("DELETE FROM fuel WHERE id = ?", [id]);
                }
                )
}

function createFuelItem(fuelItem)
{
    _db.transaction(
                function(tx){
                    tx.executeSql("INSERT INTO fuel (cost,qty, date, title, note) VALUES(?,?,?,?,?)",[fuelItem.cost,fuelItem.qty, fuelItem.date, fuelItem.title, fuelItem.note]);
                }
                )
}

function readAllFuel(model)
{
    model.clear()
    _db.readTransaction(
                function(tx){
                    var rs = tx.executeSql("SELECT * FROM fuel");
                    for (var i=0; i< rs.rows.length; i++) {
                        model.append(rs.rows.item(i));
                    }
                }
                );
}

function readFuelDate(model, date)
{
    model.clear()

    _db.readTransaction(
                function(tx){
                    var rs = tx.executeSql("SELECT * FROM fuel WHERE date =?", [date]);
                    for (var i=0; i< rs.rows.length; i++) {
                        model.append(rs.rows.item(i));

                    }
                }
                );
}

function readFuelItem(fuelId) {
    var data = {}
    _db.readTransaction(
                function(tx){
                    var rs = tx.executeSql("SELECT * FROM fuel WHERE id=?", [fuelId])
                    if(rs.rows.length === 1) {
                        data = rs.rows.item(0)
                        //data.done = data.done===0?false:true
                        //data.modified = new Date(data.modified)
                    }
                }
                )
    return data;
}

function printObject(o)
{
    print('---')
    print(Object.keys(o))
    for(var key in o) {
        print("  " + key +"="+o[key])
    }

}

function countDate(date)
{
    var data = 0;
    _db.readTransaction(
                function(tx){
                    var rs = tx.executeSql("SELECT count(*) FROM fuel WHERE date = ?",[date]);
                    if(rs.rows.length === 1) {
                        data = rs.rows.item(0)["count(*)"]
                    }

                }
                )
    return data;


}


/*
function clearArchive(model)
{
    model.clear()
    _db.transaction(
                function(tx){
                    tx.executeSql("DELETE FROM todo WHERE box = 4");
                }
                )
}
*/
/*
function updateArchive(box)
{
    _db.transaction(
                function(tx){
                    tx.executeSql("UPDATE todo SET box = 4 WHERE done = 'true' AND box=?",[box]);
                }
                )
}
*/
function defaultItem()
{
    var dt;
    var d = new Date();
    dt = d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear();
    return {date: dt, cost: 0.0, qty:0, title: "", note: ""}
}
